package com.hzzc.core.controller.back;

import com.hzzc.BizException;
import com.hzzc.Result;
import com.hzzc.api.SSOResourceTreeView;
import com.hzzc.core.service.ResourceService;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 后台资源管理
 *
 * @author Administrator
 */
@RestController
@RequestMapping("/back")
public class BackResourceController {

    @Autowired
    private ResourceService resourceService;

    /**
     * 获取资源树
     *
     * @param appId
     * @return
     */
    @GetMapping("/resources")
    public Result<List<SSOResourceTreeView>> resources(@RequestParam("appId") long appId) {
        return Result.success(resourceService.getAppResourceView(appId), "");
    }

    /**
     * 添加资源
     *
     * @param ssoResourceBean
     * @return
     * @throws BizException
     */
    @PostMapping("/resource")
    public Result<List<SSOResourceTreeView>> addResource(@RequestBody SSOResourceBean ssoResourceBean) throws BizException {
        if (ssoResourceBean.getId() != null) {
            resourceService.updateResource(ssoResourceBean.getId(), ssoResourceBean.getName(), ssoResourceBean.getAppId(), ssoResourceBean.getParentId(), ssoResourceBean.getLocator(), ssoResourceBean.getCode(), ssoResourceBean.getMsg(), ssoResourceBean.isActionFlag());
        } else {
            resourceService.addResource(ssoResourceBean.getName(), ssoResourceBean.getAppId(), ssoResourceBean.getParentId(), ssoResourceBean.getLocator(), ssoResourceBean.getCode(), ssoResourceBean.getMsg(), ssoResourceBean.isActionFlag());
        }
        return Result.success(resourceService.getAppResourceView(ssoResourceBean.getAppId()), "");
    }

    /**
     * 删除资源
     *
     * @return
     * @throws BizException
     */
    @DeleteMapping("/delResource")
    public Result<List<SSOResourceTreeView>> delResource(@RequestParam("app") Long appId, @RequestParam("resourcesId") Long resourcesId) throws BizException {
        resourceService.delResource(resourcesId);
        return Result.success(resourceService.getAppResourceView(appId), "");
    }

    @Data
    static class SSOResourceBean {

        private Long id;
        private String name;
        private Long appId;
        private Long parentId;
        private String code;
        private String locator;
        private String msg;
        private boolean actionFlag;


    }

}
